import React, { Component } from 'react';
import PropTypes from 'prop-types';
import utils from 'utils/index';
import { HrefLink } from 'utils/router';
import intl from '../../../../../../../../../locale';

const { pathResource } = window.FORCE;

export default class BranchName extends Component {
  static propTypes = {
    branchType: PropTypes.string,
  };

  static defaultProps = {
    // 获取的分支类型: source_branch 源分支， target_branch 目标分支
    branchType: 'source_branch',
  };

  getCRContentByType(content, type) {
    if (type === 'COMMIT') {
      return utils.showShortSha(content);
    } else {
      return content;
    }
  }

  render() {
    const { codeReviewDetail, sourceBranchExist, targetBranchExist, branchType } = this.props;
    if (branchType === 'source_branch') {
      return (
        <React.Fragment>
          {sourceBranchExist === 'false' ? (
            <span className="text-black">
              {this.getCRContentByType(
                codeReviewDetail.source_branch,
                codeReviewDetail.source_type,
              )}
            </span>
          ) : (
            <HrefLink
              className="text-black branch-more-style-cut"
              target="_blank"
              href={`/${pathResource.fullPath}/tree/${utils.revisionEncodeURI(
                codeReviewDetail.source_branch,
              )}`}
              rel="noreferrer"
            >
              {this.getCRContentByType(
                codeReviewDetail.source_branch,
                codeReviewDetail.source_type,
              )}
            </HrefLink>
          )}

          {codeReviewDetail.local_branch ? (
            <span className="text-black"> {`(${codeReviewDetail.local_branch})`}</span>
          ) : null}
          {sourceBranchExist === 'false' ? (
            <span className="text-grey">
              {intl.get({
                id: 'code-assets.components.branch.BranchDeleted',
                defaultMessage: '(分支已删除)',
              })}
            </span>
          ) : null}
        </React.Fragment>
      );
    } else {
      return (
        <React.Fragment>
          {targetBranchExist !== 'false' ? (
            <HrefLink
              className="text-black branch-more-style-cut"
              target="_blank"
              href={`/${pathResource.fullPath}/tree/${utils.revisionEncodeURI(
                codeReviewDetail.target_branch,
              )}`}
              rel="noreferrer"
            >
              {this.getCRContentByType(
                codeReviewDetail.target_branch,
                codeReviewDetail.target_branch,
              )}
            </HrefLink>
          ) : (
            <span className="text-black">
              {this.getCRContentByType(
                codeReviewDetail.target_branch,
                codeReviewDetail.target_branch,
              )}
            </span>
          )}

          {targetBranchExist === 'false' ? (
            <span className="text-grey">
              {intl.get({
                id: 'code-assets.components.branch.BranchDeleted',
                defaultMessage: '(分支已删除)',
              })}
            </span>
          ) : null}
        </React.Fragment>
      );
    }
  }
}
